package org.mzyyn.qr.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author lipeng
 * @description 分享图
 * @date 2024/7/8 15:03
 */
public class ShareUtil {
    /**
     * 图片宽高
     */
    public static final Integer imgWidthHeight = 576*2;
    public static void generateImg() throws IOException {
        // 获取当前线程的ClassLoader
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        // 使用ClassLoader获取类路径下的文件的InputStream
        InputStream inputStream = classLoader.getResourceAsStream("img/share1.png");

        if (inputStream == null) {
            // 文件不存在于类路径下
            System.err.println("文件未找到");
            return;
        }
        // 加载背景图
        BufferedImage background = ImageIO.read(inputStream);

        // 创建一个 Graphics2D 对象，用于绘制
        Graphics2D g2d = background.createGraphics();

        // 设置抗锯齿属性
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 使用ClassLoader获取类路径下的文件的InputStream
        InputStream inputStream1 = classLoader.getResourceAsStream("img/result.png");
        // 在背景图上绘制其他图片
        BufferedImage overlayImage = ImageIO.read(inputStream1);

        // 缩放并裁剪图片到 500x500 像素
        overlayImage = scaleImage(overlayImage, imgWidthHeight, imgWidthHeight);

        //图片圆角

        // 创建一个与overlayImage大小相同的透明图片作为遮罩
        BufferedImage mask = new BufferedImage(overlayImage.getWidth(), overlayImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D mg = mask.createGraphics();
        mg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        mg.setColor(Color.RED);
        mg.fillRoundRect(0, 0, overlayImage.getWidth(), overlayImage.getHeight(), 150, 150);
        mg.dispose();

        // 应用遮罩到图片
//        overlayImage = applyMask(overlayImage, mask);

        //图片圆角


        System.out.println(background.getWidth());

        int x = (background.getWidth() - overlayImage.getWidth()) / 2; // 居中计算
        int y = 223*2; // 居中计算
//        g2d.drawImage(overlayImage, x, y, null);



        // 在背景图上绘制文字
        g2d.setFont(new Font("Arial", Font.BOLD, 48)); // 设置字体
        g2d.setColor(Color.BLACK); // 设置颜色
        String text = "假如没有庄国栋...";
        FontMetrics fontMetrics = g2d.getFontMetrics();
        int textWidth = fontMetrics.stringWidth(text);
        int textHeight = 34*2;
        x = (background.getWidth() - textWidth) / 2; // 文字居中计算
        y = 500*2; // 文字居中计算
        g2d.drawString(text, x, y);

        // 释放资源
        g2d.dispose();
        inputStream.close();
        inputStream1.close();

        // 将最终的 BufferedImage 写入文件
        ImageIO.write(background, "png", new File("l.png"));
    }

    // 缩放并裁剪图片到指定大小
    private static BufferedImage scaleAndCrop(BufferedImage image, int targetWidth, int targetHeight) {
        int width = image.getWidth();
        int height = image.getHeight();
        double ratio = targetWidth / (double) targetHeight;

        int newWidth, newHeight;
        if (width / (double) height > ratio) {
            newWidth = (int) (height * ratio);
            newHeight = height;
        } else {
            newWidth = width;
            newHeight = (int) (width / ratio);
        }

        BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = scaledImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(image, 0, 0, newWidth, newHeight, null);
        g.dispose();

        BufferedImage croppedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        g = croppedImage.createGraphics();
        g.drawImage(scaledImage, (newWidth - targetWidth) / 2, (newHeight - targetHeight) / 2, null);
        g.dispose();

        return croppedImage;
    }

    // 缩放图片到指定大小
    private static BufferedImage scaleImage(BufferedImage image, int targetWidth, int targetHeight) {
        BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = scaledImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(image, 0, 0, targetWidth, targetHeight, null);
        g.dispose();
        return scaledImage;
    }

    // 应用遮罩到图片
    private static BufferedImage applyMask(BufferedImage image, BufferedImage mask) {
        BufferedImage maskedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D mg = maskedImage.createGraphics();
        mg.setComposite(AlphaComposite.DstOut);
        mg.drawImage(image, 0, 0,null);
        mg.drawImage(mask, 0, 0,null);
        mg.dispose();
        return maskedImage;
    }
}
